﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ReportlossesModelCommand;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.StoreCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using B.S.BaseData.Manage.Domain.Entities;
using DAL;
using MediatR;
using Microsoft.Data.SqlClient;

namespace B.S.BaseData.Manage.Api.Read.Apilaction.CoomandHandler.ReportlossesModelCommandHandler
{
    /// <summary>
    /// 报损id查询反填
    /// </summary>
    public class ReportlossesFileModelCommandHandler : IRequestHandler<ReportlossesFileModelCommand, ApiReqult<List<ReportlossesModel>>>
    {
        private readonly DBHelper bHelper;

        public ReportlossesFileModelCommandHandler(DBHelper bHelper)
        {
            this.bHelper = bHelper;
        }
        /// <summary>
        /// 报损id查询反填
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiReqult<List<ReportlossesModel>>> Handle(ReportlossesFileModelCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiReqult<List<ReportlossesModel>>();

            try
            {
                string sql = $"SELECT * from ReportlossesModel WHERE Id = '{request.Id}' AND IsDelete = 0;";
                List<ReportlossesModel>? hoModes = bHelper.GetList<ReportlossesModel>(sql);

                var managementModel = bHelper.GetList<ManagementModelDTO>(sql);

                if (managementModel != null)
                {
                    result.Data = hoModes;
                    result.Msg = ApiEnum.查询成功.ToString();
                }
                else
                {
                    result.Msg = ApiEnum.查询失败.ToString();
                    result.Code = ApiEnum.查询失败;
                }

            }
            catch (SqlException ex)
            {
                result.Msg = ApiEnum.查询失败.ToString();
                result.Code = ApiEnum.查询失败;
            }

            return result;
        }
    }
}
